<?php
/**
 * 主讲老师登陆验证
 * @author gary
 **/
class Services_Auth_Teacher extends Services_Common
{

    public static function factory()
    {
        return new self();
    }

    public function __construct()
    {
        $this->_memcache = Fend_Cache_Memcache::Factory()->getObj();
    }

    public function checkLogin()
    {
        $userinfo = array();
        if(empty($_COOKIE['teacher'])){
            return false;
        }else{
            $redis      = Fend_Cache_Redis::factory('sdkcache');
            $authorkey  = $_COOKIE['teacher'];
            $userinfo   = $redis->get('teacher_online_'.$authorkey);
        }
        //解决新旧系统同步登陆的问题
        if(empty($userinfo)){
            $string     = $this->_memcache->get($_COOKIE['teacher']);
            $userinfo   = self::decodeMemcache($string);
        }
        if(empty($userinfo['id']) && !empty($userinfo['teacherId'])){
            $tchinfo    = Services_Api_Teacher::factory()->getTeacherInfoById($userinfo['teacher_user_id']);
            if(!empty($tchinfo['data'])){
                $userinfo  = array_merge($userinfo,$tchinfo['data']);
                $redis->set('teacher_online_'.$authorkey,$userinfo);
            }
        }
        $this->teacher  = $userinfo;
        return $userinfo;
    }
    /**
     * 通过用户名和密码登陆
     * @param string $username 用户名
     * @param string $password 密码
     * @param int $tchtype     身份类型,1-主讲,其他辅导
     *
     * @return mixed
     */
    public function login($username='',$password='',$tchtype=1)
    {
        $redis          = Fend_Cache_Redis::factory('sdkcache');
        if(empty($username) || empty($password)){
            return array('stat'=>0,'data'=>"用户名与密码异常");
        }
        $authorkey      = $this->createId();

        $backdata       = Services_Api_Admin::factory()->login($username,$password);
        $logininfo = $backdata['data'];
        //如果登陆信息为空,直接返回信息
        if(empty($logininfo['adminId'])){
            return $logininfo;
        }
        if($tchtype==1){
            //全职主讲
            $tchid = !empty($logininfo['teacherId'])?$logininfo['teacherId']:'';
        }elseif(in_array($tchtype,array(2,4,5))){
            //兼职主讲,辅导老师,辅导外教
            $tchid = !empty($logininfo['counselorId'])?$logininfo['counselorId']:'';
        }
        if(empty($tchid)){
            return array('stat'=>0,'data'=>"此老师ID不存在或老师身份不匹配");
        }
        $tchinfo        = Services_Api_Teacher::factory()->getTeacherInfoById($tchid);
        if(empty($tchinfo['data'])){
            return array('stat'=>0,'data'=>"教师信息不存才");
        }
        $userinfo   = array_merge($logininfo,$tchinfo['data']);
        $logininfo['teacher_user_id'] = $tchinfo['data']['id'];
        $redis->set('teacher_online_'.$authorkey,$userinfo);

        foreach($logininfo as $key=>$vs){
            $arr[] = $key.'|'.serialize($vs);
        }
        $string = join('',$arr);
        $this->_memcache->set($authorkey,$string);
        $this->teacher      = $tchinfo['data'];
        $cookiedomian   = !empty(self::$apiurl['Services_Api_Admin']['cookiedomain'])?self::$apiurl['Services_Api_Admin']['cookiedomain']:'.xueersi.com';
        FendHttp::setCookie('teacher',$authorkey,time(),'/',$cookiedomian);
        return true;
    }

    /**
     * 通过token登陆系统
     * @param string $token 用户名
     * @param int $tchtype     身份类型,1-主讲,其他辅导
     *
     * @return mixed
     */
    public function loginToken($token,$tchtype=1)
    {
        $redis          = Fend_Cache_Redis::factory('sdkcache');
        if(empty($token)){
            return array('stat'=>0,'msg'=>"token不能为空");
        }
        $authorkey      = md5($token);
        $cookiedomian   = !empty(self::$apiurl['Services_Api_Admin']['cookiedomain'])?self::$apiurl['Services_Api_Admin']['cookiedomain']:'.xueersi.com';
        FendHttp::setCookie('teacher',$authorkey,time(),'/',$cookiedomian);
        $backdata      = Services_Api_Admin::factory()->checkLogin($token);
        print_R($backdata);
        $logininfo     = $backdata['data'];
        if(empty($logininfo['adminId'])){
            return $logininfo;
        }
        if($tchtype==1){
            //全职主讲
            $tchid = !empty($logininfo['teacherId'])?$logininfo['teacherId']:'';
        }elseif(in_array($tchtype,array(2,4,5))){
            //兼职主讲,辅导老师,辅导外教
            $tchid = !empty($logininfo['counselorId'])?$logininfo['counselorId']:'';
        }
        unset($logininfo);
        if(empty($tchid)){
            return array('stat'=>0,'msg'=>"此老师ID不存在或老师身份不匹配");
        }
        $tchinfo        = Services_Api_Teacher::factory()->getTeacherInfoById($tchid);

        $redis->set('teacher_online_'.$authorkey,$tchinfo['data']);
        $this->teacher      = $tchinfo;

        return true;
    }


}